package com.timoon.component.panel.tree.ztree.checkboxtree;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;

import com.googlecode.wicket.jquery.ui.Options;
import com.timoon.app.utils.GlobalRandom;
import com.timoon.app.utils.HttpServletUtil;
import com.timoon.component.behavior.ztree.ZTreeWithCheckboxBehavior;
import com.timoon.component.util.ComponentConstant;

/**
 *<p>带复选框的ztree</p>
 *<p></p>
 *<p>Copyright:Copyright(c)2013.4.1</p>
 *<p>Company:timoon</p>
 *@author 付乐天
 *@version 1.0 */
public class ZTreeCheckboxPanel extends Panel {
	private static final long serialVersionUID = 5180713832181389133L;
	
	private Integer key=GlobalRandom.getRandom();
	
	private IModel<String[]> model;
	
	private AttributeModifier onclickJS;
	/**
	 * 
	 * @param id
	 * @param button 提交表单的按钮
	 * @param call panel获取到前台发送过来的值后会调用
	 */
	public ZTreeCheckboxPanel(String id,IModel<String[]> model,CheckboxParameter checkboxParameter) {
		super(id,model);
		// TODO Auto-generated constructor stub
		this.model=model;
		
		this.regist();
		
		/*
		 * 为<ul>创建随机id，初始化ul
		 * */
		WebMarkupContainer ztree=new WebMarkupContainer("wk_ztree");
		add(ztree);
		ztree.add(new AttributeModifier("id", "ztree-"+key));
		
		Options opt=new Options();
		opt.set("zNodes", checkboxParameter.getJSON());
		this.add(new ZTreeWithCheckboxBehavior("#ztree-"+key,opt));
		
		
		//绑定submitButton的点击事件
		onclickJS=new AttributeModifier("onclick","myztreeGetCheckedIds('"+"ztree-"+key+"','"+key+"','"+HttpServletUtil.getBasePath()+"')");
	}
	public void onChecked(String[] ids){
		model.setObject(ids);
	};
	//在controller中注册自己
	private void regist(){
		
		HttpSession session=HttpServletUtil.getHttpSession();
		Map<String,ZTreeCheckboxPanel> map=(Map<String, ZTreeCheckboxPanel>) session.getAttribute(ComponentConstant.ZTREE_CHECKBOX_PANEL_MAP_IN_SESSION);
		if(map==null){
			map=new HashMap<String,ZTreeCheckboxPanel>();
		}
		map.put(String.valueOf(key), this);
		session.setAttribute(ComponentConstant.ZTREE_CHECKBOX_PANEL_MAP_IN_SESSION, map);
	}
	public AttributeModifier getOnclickJS() {
		return onclickJS;
	}

}
